package Algorithm.sort;

public class Merge_sort {
    public void sort(int[] nums,int start,int end){
        if (start>=end){
            return;
        }

        int mid = (start+end)/2;
        sort(nums,start,mid);
        sort(nums,mid+1,end);
        merge(nums,start,mid,end);
    }

    public void merge(int[] nums,int start,int mid,int end){
        int[] temp = new int[end-start+1];
        int i = start,j = mid+1;
        int k = 0;
        while (i<=mid && j<=end){
            if (nums[i]<=nums[j]){
                temp[k++]=nums[i++];
            }else {
                temp[k++] = nums[j++];
            }
        }
        while (i<=mid){
            temp[k++] = nums[i++];
        }
        while (j<=end){
            temp[k++] = nums[j++];
        }
        System.arraycopy(temp, 0, nums, start, temp.length);
    }
}
